﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

using Ccit.Utils;
using Ccit.OA.MsSystemDoc;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace Ccit.OA.SystemDoc
{
    public partial class SystemDoc_docmanage_upload : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                ViewState["folderId"] = (null != Session["folderId"]) ? Session["folderId"].ToString() : "1";
                ViewState["classType"] = (null != Session["classType"]) ? Session["classType"].ToString() : "001";
                btnConfirm.Attributes.Add("onclick", "return doUpload()");
                inpage();
            }
        }

        private void inpage()
        { 
            string sClassType,sWhere,sCode;
            sClassType = ViewState["classType"].ToString().Trim();
            SystemDocBLL bll = new SystemDocBLL();
            sWhere = " and DOC_B2_10 = " + ViewState["folderId"].ToString().Trim();
            List<MsSecondClass> msList = bll.GetMsSecondItem(sWhere);
            if (msList.Count != 0)
            {
                lbType.Text = msList[0].Systypename;
            }
            sCode = bll.GetEncodRules(sClassType,msList[0].Sysnumber);
            if (sCode != "") {
                txtNumbers.Value = sCode;
                txtNumbers.Attributes.Add("readonly", "TRUE"); 
            }
            msList = null;
            bll = null;
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            string sMsg = string.Empty;
            string sFileName = string.Empty;

            string serAttach = string.Empty;
            if (doSave())
            {
                //string encodeparam = string.Empty;
                //encodeparam = Base64Lib.Encode("classType=" + hidClassType.Value.Trim() + "&Field=" + ID);
                Response.Write("<script>alert('文件上传成功!');window.location.href='SystemDoc_docmanage_upload.aspx';</script>");
            }
            else
            {
                sMsg = "操作失败,请重试!";
            }

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            string encodeparam;
            encodeparam = Base64Lib.Encode("classType=" + ViewState["classType"].ToString().Trim());
            Response.Redirect("SystemDoc_docmanage_list.aspx?" + encodeparam);
        }

        private bool doSave()
        {
            string sFolderId = ViewState["folderId"].ToString().Trim();
            int sGuid = 0;
            string cmdText = "";
            int iExecuteRow = 0;
            //获取文件对象
            HttpPostedFile oPf = MyFile.PostedFile;
            //获取大小
            int iContentLength = oPf.ContentLength;
            Byte[] oByte = new byte[iContentLength];

            #region 文件基础信息 插入
            LoginUser loginUser = Session["LogUserInfo"] as LoginUser;

            MsDocClass ms = new MsDocClass();
            ms.sysdoctitle = GetFileName("Full");
            ms.syscode = txtNumbers.Value.Trim();
            ms.systype = ms.sysdoctitle.Substring(ms.sysdoctitle.LastIndexOf(".") + 1).ToLower();
            ms.syslength = iContentLength.ToString().Trim();
            ms.sysecond = ViewState["folderId"].ToString().Trim();
            ms.sysremarks = this.txtremark.Text;
            ms.updatetime = ViewState["classType"].ToString().Trim();

            ms.jobId = loginUser.PosId;
            ms.deptId = loginUser.DeptId;
            ms.cjuserId = loginUser.UserId;
            ms.levelId = loginUser.ClassId;
            ms.agencId = loginUser.OrganId;
            ms.cjdatetime = System.DateTime.Now.ToString();

            SystemDocBLL bll = new SystemDocBLL();
            ms = bll.uploadFile(ms);
            #endregion
            //获取文件流
            Stream oStream = oPf.InputStream;
            oStream.Read(oByte, 0, iContentLength);
            sGuid = int.Parse(ms.sysdocid);
            cmdText = "INSERT INTO DOC_B7(DOC_B7_10,DOC_B7_20,DOC_B7_30) VALUES(#sGuid,#sVersion,#sContent)";

            IDataParameter guid = DataAccessor.GetDataParameter("sGuid", DataType.Int);
            IDataParameter version = DataAccessor.GetDataParameter("sVersion", DataType.Int);
            IDataParameter content = DataAccessor.GetDataParameter("sContent", DataType.Binary);

            guid.Value = sGuid;
            version.Value = int.Parse("1");
            content.Value = oByte;

            IDataParameter[] parameters = new IDataParameter[] { guid, version, content };
            iExecuteRow = DataAccessor.ExecuteNonQuery(cmdText, parameters);

            ms = null;
            if (iExecuteRow > 0)
            {
                ViewState["GUID"] = sGuid;
                ViewState["FileLength"] = iContentLength.ToString();
                return true;
            }
            else
                return false;
        }

        private string GetFileName(string sType)
        {
            HttpPostedFile oPf = MyFile.PostedFile;
            string sFileName = oPf.FileName;
            int iPostion = 0;

            iPostion = sFileName.LastIndexOf("\\");

            sFileName = sFileName.Substring((iPostion + 1));

            if (sType != "Full")   //不带扩展名的文件名
            {
                iPostion = sFileName.LastIndexOf(".");
                if (iPostion != -1)
                {
                    sFileName = sFileName.Substring(0, iPostion);
                }
            }
            return sFileName;
        }
    }
}
